package com.common.adapter.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by MrWang on 2016/7/1.
 */
public abstract class CommonAdapter<T> extends BaseAdapter {
    protected Context mContext;
    protected List<T> mList;
    protected LayoutInflater mInflater;
    private int mLayoutID;

    public CommonAdapter(Context ctx, List<T> list, int layoutID) {
        this.mContext = ctx;
        this.mList = list;
        this.mLayoutID = layoutID;
        this.mInflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return null == mList ? 0 : mList.size();
    }

    @Override
    public T getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        CommonHolderView holderView = CommonHolderView.get(mContext, convertView, parent, mInflater, mLayoutID, position);

        convertView(holderView, getItem(position), position);

        return holderView.getConvertView();
    }

    protected abstract void convertView(CommonHolderView holder, T t, int position);
}
